package zero;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.PriorityBlockingQueue;

//模拟实现一个线程池（线程数目固定）
class MythreadPool{
    //创建工作队列
   BlockingQueue<Runnable> queue=null;
    //创建线程池
    public MythreadPool(int n){
        queue=new ArrayBlockingQueue<>(1000);
        for(int i=0;i<n;i++){
            Thread t=new Thread(()->{
                while(true){
                    Runnable task= null;
                    try {
                        task = queue.take();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    task.run();
                }

            });
            t.start();
        }

    }
    //将任务放入工作队列中
    public void submit(Runnable task) throws InterruptedException {
        queue.put(task);
    }
}

public class Test {
    public static void main(String[] args) throws InterruptedException {
        MythreadPool mythreadPool=new MythreadPool(10);
        for(int i=0;i<100;i++){
            int id=i;
            mythreadPool.submit(()->{
                System.out.println(Thread.currentThread().getName()+" "+id);


            });
        }



    }
}
